package com.sjgtw.cloud.config;

import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection;
import com.mapbox.geojson.LineString;
import com.sjgtw.cloud.util.FileUtil;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

import java.io.InputStream;

@Service
public class LineStringService implements ApplicationListener<ContextRefreshedEvent> {

    private LineString lineString;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {

        InputStream fis = LineStringService.class.getClassLoader().getResourceAsStream("zhunchiline.json");
        String jsonString = FileUtil.readFile(fis);

        // 解析 JSON 并转换为 FeatureCollection
        FeatureCollection featureCollection = FeatureCollection.fromJson(jsonString);

        // 遍历 FeatureCollection 中的每个 Feature
        for (Feature feature : featureCollection.features()) {
            if (feature.geometry() instanceof LineString) {
                LineString lineString = (LineString) feature.geometry();
                this.lineString = lineString;
            }
        }

    }

    public LineString getLineString() {
        return lineString;
    }

}
